package com.ludomancy.hugealien;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import com.ludomancy.gamelib.Accumulator;
import com.ludomancy.gamelib.ImageAnimation;
import com.ludomancy.gamelib.MathHelper;
import com.ludomancy.gamelib.Randomizer;
import com.ludomancy.gamelib.Sound;

public class Uruguayan extends BasicEntity
{
	public Point house;
	private ImageAnimation anim;
	private ImageAnimation animleft;
	private Accumulator pauseBetweenWindow = new Accumulator(0);
	public boolean inWindow = false;
	private ImageAnimation current;
	public Soul soul;
	private Window window;
	private Balloon expression;
	private boolean sideRight = false;
	
	public Uruguayan(Point pos, Soul s)
	{
		house = pos;
		soul = s;
		anim = ImageAnimation.create("inhouse.png", 12, 14, 4);
		animleft = ImageAnimation.create("inhouse-left.png", 12, 14, 4);
		animleft.reverse();
		x = pos.x*City.TILE_WIDTH + City.TILE_WIDTH/2;
		y = pos.y*City.TILE_HEIGHT + City.TILE_HEIGHT/2;
		window = new Window(Window.TYPE_INHABITED, pos.x, pos.y);
		pauseBetweenWindow.reset(Randomizer.getRandom(1000,3000));
		expression = new Balloon(this);
		Game.game.city.entities.add(expression);
		sideRight = Randomizer.getRandom(0,1) == 0;
	}
	
	public static float dist(Uruguayan u1, Uruguayan u2)
	{
		return MathHelper.dist(u1.x, u1.y, u2.x, u2.y);
	}
	
	public Rectangle area()
	{
		return new Rectangle(x - City.TILE_WIDTH/2, y - City.TILE_HEIGHT/2, City.TILE_WIDTH, City.TILE_HEIGHT);
	}
	
	public void paint(Graphics g, int px, int py)
	{
		window.paint(g, area().x, area().y);
		if(inWindow && !current.isOver())
		{
			current.paintCurrentFrame(g, area().x + 4, area().y + 4);
		}
	}
	
	public void update()
	{
		if(Game.game.isOver && soul.getMood() != Soul.MOOD_OK)
		{
			expressMood();
		}
		// speed up to mood
		int speed = 4;
		if(soul.getMood() == Soul.MOOD_HAPPY)
		{
			speed = 3;
		}
		else
		if(soul.getMood() == Soul.MOOD_HEARTBROKEN)
		{
			speed = 6;
		}
		else
		if(soul.getMood() == Soul.MOOD_SHOCKED)
		{
			speed = 0;
		}
		animleft.frameTime = speed;
		soul.update();
		animleft.update();
		anim.update();
		if(inWindow && anim.isOver() && animleft.isOver())
		{
			pauseBetweenWindow.reset(Randomizer.getRandom(700,1200));
			inWindow = false;
		}
		pauseBetweenWindow.update();
		if(pauseBetweenWindow.isDone() && !inWindow)
		{
			if(Game.game.isOver)
			{
				return;
			}
			inWindow = true;
			anim.rewind();
			anim.play();
			animleft.rewind();
			animleft.play();
			current = sideRight ? anim : animleft;
			sideRight = !sideRight;
			// is he having an impulse?
			if(soul.isSuicidal())
			{
				// jump out
				Sound.play("crash.wav");
				Game.game.city.killUruguayan(this);
				Game.game.city.entities.add(new Suicide(x,y,soul));
			}
			else
			{
			}
		}
		if(soul.getMood() != Soul.MOOD_OK)
		{
			expressMood();
		}

	}

	public void expressMood()
	{
		expression.express();
	}

	public void remove()
	{
		Game.game.city.entities.remove(this);
		Game.game.city.entities.remove(expression);
	}
}
